package lxs.tool.liberator.common.base.aspect;

import lxs.tool.liberator.common.base.result.Response;
import lxs.tool.liberator.common.exception.ConnectException;
import lxs.tool.liberator.system.config.DynamicDataSource;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component()
public class Aop {
    @Autowired
    private DynamicDataSource dbSourceService;
    @Around("execution(public lxs.tool.liberator.common.base.result.Response *.*(..))")
    public Response around(ProceedingJoinPoint joinPoint) throws Throwable {
        Response re;
        try {
            re = (Response) joinPoint.proceed();
        } catch (Exception e) {
            dbSourceService.switchBack();
            e.printStackTrace();
            if (e instanceof ConnectException) {
                re = Response.error(e.getMessage()).setCode(3);
            } else re = Response.error(e.getMessage());
        }
        return re;
    }
}
